[Chapter 7] Macros
Author: Bryan Costales & Eric Allman
Date: 1999-01-06T18:35:10Z

#

| Chapter 7 |

# 7. Macros

Contents:
Overview
Defining Macros
Predefined Macros
Things to Try

One of the major strengths of the sendmail.cf file is that it allows text to be referenced symbolically. This is very similar to how variables in the Bourne and C shells are used:

>     REMOTE=mailhost           Bourne shell
>     set REMOTE=mailhost       C shell
>     D{REMOTE}mailhost         sendmail.cf file

The statements above all cause a variable with the name `REMOTE` to be assigned the value `mailhost`.

The expression for using (referencing) the value stored in `REMOTE` is similar for all three:

>     $REMOTE               Bourne shell
>     $REMOTE               C shell
>     ${REMOTE}             sendmail.cf file

That is, all of the above expressions yield the value stored in `REMOTE`, in this case the text `mailhost`. Once you define the value of `REMOTE` as `mailhost`, you can use the expression `${REMOTE}` anywhere you need to use the text `mailhost`.

## 7.1 Overview

Macros can greatly simplify your work. They allow you to represent text symbolically and to define it in one central place. Changes in that text are automatically propagated to the rest of the file. For example, consider the following definition in your sendmail.cf file:

>     D{REMOTE}mailhost

If you use the expression `${REMOTE}` anywhere in that file, a single change to the definition of `D{REMOTE}mailhost` changes the value of `${REMOTE}` throughout the entire file.

Here is the format for defining macros:

>     DXtext

The letter `D` must begin the line. The `D` is immediately followed by the name of the macro, here `X`, with no intervening space. The name is then followed (again with no intervening space) by the text (value) for the macro's definition. The value is all the text up to the end of the line. [1]

> [1] Trailing whitespace is automatically stripped in V8.7 but not in earlier versions. Leading whitespace is not, so the `D`, name, and text must be right smack against each other.

Macros may have single-character names or multicharacter names. Multicharacter names must always be enclosed in curly braces. Single-character names may be enclosed in curly braces if you desire. Prior to V8.7 you could use single characters only without curly braces.

>     DRmailhost             single-character name (prior to V8.7)
>     D{R}mailhost           same beginning with V8.7
>     D{REMOTE}mailhost      multicharacter name (beginning with V8.7)

Except for header and delivery agent configuration commands, [2] macros, in general, are expanded (the text replaces the symbol) when the configuration file is read. Consequently, you must define them before you use them:

> [2] And configuration commands for which macros make no sense and are ignored.

>     D{ROLE}son
>     S${ROLE}                 use the text value ``son''
>     D{ROLE}mother
>     S${ROLE}                 use the text value ``mother''

Here, `ROLE` was first given the value `son`. When the first `S` command is processed, sendmail replaces the expression `${ROLE}` with its current value to form

>     Sson

Then `ROLE` is redefined and given a different value, that of `mother`. The second `S` command is processed, and its `${ROLE}` is replaced with the new value:

>     Smother

Please note that this is very bad style. In general, each macro should be defined only once to avoid confusion.

6.5 Things to Try |  | 7.2 Defining Macros